package com.zenithmind.news.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.news.pojo.entity.UserBehavior;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 用户行为Mapper接口
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Mapper
public interface UserBehaviorMapper extends BaseMapper<UserBehavior> {

    /**
     * 查询用户行为统计
     * 
     * @param userId 用户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 行为统计
     */
    List<BehaviorStatistics> selectUserBehaviorStatistics(@Param("userId") String userId,
                                                          @Param("startTime") LocalDateTime startTime,
                                                          @Param("endTime") LocalDateTime endTime);

    /**
     * 查询新闻行为统计
     * 
     * @param newsId 新闻ID
     * @return 行为统计
     */
    List<BehaviorStatistics> selectNewsBehaviorStatistics(@Param("newsId") String newsId);

    /**
     * 查询用户偏好分析
     * 
     * @param userId 用户ID
     * @param days 天数
     * @return 偏好分析
     */
    List<UserPreference> selectUserPreferences(@Param("userId") String userId,
                                              @Param("days") Integer days);

    /**
     * 查询活跃用户
     * 
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param limit 限制数量
     * @return 活跃用户列表
     */
    List<ActiveUser> selectActiveUsers(@Param("startTime") LocalDateTime startTime,
                                      @Param("endTime") LocalDateTime endTime,
                                      @Param("limit") Integer limit);

    /**
     * 清理过期行为数据
     * 
     * @param expireTime 过期时间
     * @return 清理数量
     */
    int deleteExpiredBehaviors(@Param("expireTime") LocalDateTime expireTime);

    /**
     * 行为统计
     */
    class BehaviorStatistics {
        private Integer behaviorType;
        private String behaviorName;
        private Long count;
        private Double totalScore;
        private Double avgScore;

        // Getters and Setters
        public Integer getBehaviorType() { return behaviorType; }
        public void setBehaviorType(Integer behaviorType) { this.behaviorType = behaviorType; }
        
        public String getBehaviorName() { return behaviorName; }
        public void setBehaviorName(String behaviorName) { this.behaviorName = behaviorName; }
        
        public Long getCount() { return count; }
        public void setCount(Long count) { this.count = count; }
        
        public Double getTotalScore() { return totalScore; }
        public void setTotalScore(Double totalScore) { this.totalScore = totalScore; }
        
        public Double getAvgScore() { return avgScore; }
        public void setAvgScore(Double avgScore) { this.avgScore = avgScore; }
    }

    /**
     * 用户偏好
     */
    class UserPreference {
        private String category;
        private String tag;
        private Long count;
        private Double score;
        private Double weight;

        // Getters and Setters
        public String getCategory() { return category; }
        public void setCategory(String category) { this.category = category; }
        
        public String getTag() { return tag; }
        public void setTag(String tag) { this.tag = tag; }
        
        public Long getCount() { return count; }
        public void setCount(Long count) { this.count = count; }
        
        public Double getScore() { return score; }
        public void setScore(Double score) { this.score = score; }
        
        public Double getWeight() { return weight; }
        public void setWeight(Double weight) { this.weight = weight; }
    }

    /**
     * 活跃用户
     */
    class ActiveUser {
        private String userId;
        private Long behaviorCount;
        private Double totalScore;
        private Double activityScore;
        private LocalDateTime lastActiveTime;

        // Getters and Setters
        public String getUserId() { return userId; }
        public void setUserId(String userId) { this.userId = userId; }
        
        public Long getBehaviorCount() { return behaviorCount; }
        public void setBehaviorCount(Long behaviorCount) { this.behaviorCount = behaviorCount; }
        
        public Double getTotalScore() { return totalScore; }
        public void setTotalScore(Double totalScore) { this.totalScore = totalScore; }
        
        public Double getActivityScore() { return activityScore; }
        public void setActivityScore(Double activityScore) { this.activityScore = activityScore; }
        
        public LocalDateTime getLastActiveTime() { return lastActiveTime; }
        public void setLastActiveTime(LocalDateTime lastActiveTime) { this.lastActiveTime = lastActiveTime; }
    }
}
